/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jtextfield.java
 *
 * Created on May 4, 2010, 7:39:59 PM
 */
package org.components.parent.controls;

import app.utils.SystemUtil;
import com.components.custom.IComponent;
import com.components.custom.IContainer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import org.components.parent.Documents.DocumentListenerx;

/**
 *
 * @author nano
 */
public class PTextField extends javax.swing.JTextField implements IComponent{

    protected  IContainer container;
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


    @Override
    protected void processFocusEvent(FocusEvent e) {
        super.processFocusEvent(e);
        if (e.isTemporary()) {
            return;
        }

        int id = e.getID();
        switch (id) {
            case FocusEvent.FOCUS_GAINED:
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        selectAll();
                    }
                });
                break;
            case FocusEvent.FOCUS_LOST:
                final PTextField org = (PTextField) e.getComponent();

                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        org.setCaretPosition(org.getText().length());
                    }
                });
                break;
        }




    }

    /** Creates new form BeanForm */
    public PTextField() {
        initComponents();
        id=SystemUtil.getKeyStr();
        addDocumentListener(docx);
        addKeyListener(keyadapter);
        
    }
    
    KeyAdapter keyadapter=new KeyAdapter() {

        @Override
        public void keyPressed(KeyEvent e) {
//            if(e.getKeyCode()==KeyEvent.VK_CONTROL)
//                System.out.println("controll pressed");
        }
    
    };

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setName("Form"); // NOI18N
    }// </editor-fold>//GEN-END:initComponents
    private String formater;

    /**
     * Get the value of formater
     *
     * @return the value of formater
     */
    public String getFormater() {
        return formater;
    }

    /**
     * Set the value of formater
     *
     * @param formater new value of formater
     */
    public void setFormater(String formater) {
        this.formater = formater;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    @Override
    public void setContainer(IContainer con) {
        this.container =con;
    }

    public IContainer getContainer() {
        return container;
    }
    
    public void addDocumentListener(DocumentListenerx docx){
        getDocument().addDocumentListener(docx);
    }
    
    DocumentListenerx docx = new DocumentListenerx() {
        @Override
        public void action(DocumentEvent e) {
            if(docAction==null || disableDoc)return;//ccc
            ActionEvent actionEvent=new ActionEvent(e,21,"docaction");
            docAction.actionPerformed(actionEvent);
        }
    };
    private ActionListener docAction;
    
    public void setDocAction(ActionListener action){
        
        docAction =action;
    }
     
    public void setEnterAction(){
    }
    
    ActionListener keyAct=new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
       
        }
    };

    boolean disableDoc=false;
    @Override
    public void setText(String t) {
        disableDoc=true;
        super.setText(t);
        disableDoc=false;
    }
    
    public String getValue(){
     String x = getText();
        if (x.isEmpty()) {
            return null;
        } else {
            return x;
        }
    }
}
